package com.tina.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tina.constant.ShopCode;
import com.tina.entity.Flowers;
import com.tina.exception.CastException;
import com.tina.exception.CustomerException;
import com.tina.mapper.FlowersMapper;
import com.tina.service.IFlowersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author Tina
 * @since 2020-10-26
 */
@Service
@Slf4j
public class FlowersServiceImpl extends ServiceImpl<FlowersMapper, Flowers> implements IFlowersService {

    @Autowired
    private FlowersMapper flowersMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Value("${mq.order.topic}")
    private String topic;

    @Value("${mq.order.tag.cancel}")
    private String tag;

    @Override
    public Integer insertFood(Flowers flowers) {
        return flowersMapper.insert(flowers);
    }

    @Override
    public Integer editFood(Flowers flowers) {
        return flowersMapper.updateById(flowers);
    }

    @Override
    public List<Flowers> selectFlowers(Flowers flowers) {
        QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
        if (flowers.getSortBy() != null) {
            if (flowers.getSortBy().equals("newDate")) {
                queryWrapper.orderByDesc("add_time");
            }
            if (flowers.getSortBy().equals("priceAsc")) {
                queryWrapper.orderByAsc("sell_price");
            }
            if (flowers.getSortBy().equals("priceDesc")) {
                queryWrapper.orderByDesc("sell_price");
            }

        }
        return flowersMapper.selectList(queryWrapper);
    }

    /**
     * ID查询商品
     *
     * @param flowersId
     * @return
     */
    @Override
    public Flowers selectFlowersById(Integer flowersId) {
        return flowersMapper.selectById(flowersId);
    }

    /**
     * 校验证商品
     * 是否下架
     * 是否存在
     * 是否库存充足
     *
     * @param flowersId
     * @return
     */
    @Override
    public void checkFood(Integer flowersId) throws CustomerException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", flowersId);
        Flowers flowers = flowersMapper.selectOne(queryWrapper);
        if (flowers != null) {
            if (flowers.getStatus() == 0) {
                CastException.cast(ShopCode.SHOP_GOODS_OFF_SHELF);
            } else if (flowers.getStock() < 1) {
                CastException.cast(ShopCode.SHOP_GOODS_NUM_NOT_ENOUGH);
            }
        } else {
            CastException.cast(ShopCode.SHOP_GOODS_NO_EXIST);
        }
        log.info("校验证“ " + flowersId + " ”商品通过");
    }

    /**
     * 扣减库存
     *
     * @param flowers 商品
     * @return
     */
    @Override
    public void reduceGoodsNum(Flowers flowers) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", flowers.getId());
        Flowers selectOne = flowersMapper.selectOne(queryWrapper);
        if (flowers.getStock() > selectOne.getStock()) {
            CastException.cast(ShopCode.SHOP_GOODS_NUM_NOT_ENOUGH);
        }
        flowers.setStock(selectOne.getStock() - flowers.getStock());
        flowersMapper.updateById(flowers);
        log.info(flowers.getId() + " 商品扣减库存成功");
    }


}
